package cn.net.shuxing.yifu.config;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import cn.net.shuxing.yifu.common.constant.FilePathContant;
import com.github.wxpay.sdk.WXPayConfig;
import lombok.Getter;
import lombok.Setter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import javax.servlet.ServletContext;

public class WXPayConfigImpl implements WXPayConfig{

    private byte[] certData;
    private static WXPayConfigImpl INSTANCE;

    @Value("${wx.pay.certPath}")
    private String certPath = "./webapps/ROOT/WEB-INF/classes/cert/apiclient_cert.p12";
//    private String certPath = "./target/classes/cert/apiclient_cert.p12";

    @Getter
    @Setter
    @Value("${wx.pay.notify_url}")
    private String notifyUrl;

    @Autowired
    private ServletContext servletContext;

    private WXPayConfigImpl() throws Exception{
//        InputStream certStream = servletContext.getResourceAsStream(certPath);
//        String realPath = servletContext.getRealPath(certPath);
//        File file = new File(realPath);
//        this.certData = new byte[(int) file.length()];
//        certStream.read(this.certData);
//        certStream.close();
        InputStream certStream = new FileInputStream(certPath);
        File file = new File(certPath);
        this.certData = new byte[(int) file.length()];
        certStream.read(this.certData);
        certStream.close();
    }

    public static WXPayConfigImpl getInstance() throws Exception{
        if (INSTANCE == null) {
            synchronized (WXPayConfigImpl.class) {
                if (INSTANCE == null) {
                    INSTANCE = new WXPayConfigImpl();
                }
            }
        }
        return INSTANCE;
    }

    public String getAppID() {
        return "wx0f56cfe2aeb22164";
    }

    public String getMchID() {
        return "1526495321";
    }

    public String getKey() {
        return "BHXQKE4QEAOJKGWI7UDOJD4C5V6UKFBA";
    }

    public String getNotifyUrl() {
        return "http://47.98.205.250/front/notify/wxpay";
    }

    public String getTradeType() {
        return "APP";
    }

    public InputStream getCertStream() {
        ByteArrayInputStream certBis;
        certBis = new ByteArrayInputStream(this.certData);
        return certBis;
    }


    public int getHttpConnectTimeoutMs() {
        return 2000;
    }

    public int getHttpReadTimeoutMs() {
        return 10000;
    }

//      IWXPayDomain getWXPayDomain() {
//          return WXPayDomainSimpleImpl.instance();
//      }

    public String getPrimaryDomain() {
        return "api.mch.weixin.qq.com";
    }

    public String getAlternateDomain() {
        return "api2.mch.weixin.qq.com";
    }

    public int getReportWorkerNum() {
        return 1;
    }

    public int getReportBatchSize() {
        return 2;
    }

    public String getSpbillCreateIp() {
        return "47.98.205.250";
    }
}